package com.s666.demo.utils;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 *
 */
@Component
public class Pager {
	/**
	 * 获取get参数查询的字符串
	 *
	 * @param request
	 * @return
	 */
	public String getLink(HttpServletRequest request) {
		StringBuffer url = request.getRequestURL();
		Map<String, String[]> params = request.getParameterMap();
		StringBuffer queryString = getQueryString(params);

		url.append("?");
		url.append(queryString);
		return url.toString();
	}

	private StringBuffer getQueryString(Map<String, String[]> params) {
		StringBuffer sBuffer = new StringBuffer(64);
		for (Map.Entry<String, String[]> entry : params.entrySet()) {
			String key = entry.getKey();
			if("page".equals(key)){
				continue;
			}
			String[] values = entry.getValue();
			for (int i = 0; i < values.length; i++) {
				String value = values[i];
				sBuffer.append(key);
				sBuffer.append("=");
				sBuffer.append(value);
				sBuffer.append("&");
			}
		}
		return sBuffer;
	}

}
